package com.example.exception;


import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalDefultExceptionHandler {

    //声明要捕获的异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String defultExcepitonHandler(HttpServletRequest request, Exception e) {
        if(e instanceof BindException){
            BindingResult bindingResult = ((BindException) e).getBindingResult();
            bindingResult.getFieldError().getDefaultMessage();
            System.out.println( bindingResult.getFieldError().getDefaultMessage());
        }
        e.printStackTrace();
        return "err";
    }
}